<template>
  <div class="login-container">
    <div class="login-box">
      <h2>Login</h2>
      <form @submit.prevent="login">
        <input v-model="email" type="email" placeholder="Email" required />
        <input v-model="password" type="password" placeholder="Password" required />
        <button type="submit">Login</button>
      </form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      email: '',
      password: ''
    };
  },
  methods: {
    async login() {
      try {
        const response = await axios.post('http://localhost:8000/api/login/', {
          email: this.email,
          password: this.password
        });

        // 登录成功
        console.log('登录成功:', response.data);
        localStorage.setItem('token', response.data.token);  // 存储 token
        this.$router.push('/index');  // 跳转到首页
      } catch (error) {
        alert('请检查邮箱或密码');
        // 显示后端返回的错误信息
        //const errorMessage = error.response?.data?.error || '登录失败，请检查邮箱或密码';
        //alert(errorMessage);  // 或者用 Element UI 的 this.$message.error
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #ffeef9; /* 可选：外层浅色背景，对比更明显 */
}

.login-box {
  width: 400px;
  height: 500px;
  background: linear-gradient(to bottom right, #ff9a9e, #fad0c4); /* 粉红渐变背景 */
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  padding: 40px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: white; /* 文字颜色改为白色以便看清 */
}
.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #ff6f91;
}

.login-box input {
  width: 100%;
  height: 25px;
  margin-bottom: 20px;
  padding: 8px;
  font-size: 14px;
  border: 1px solid #ddd;
  border-radius: 6px;
}

.login-box button {
  width: 100%;
  height: 30px;
  background-color: #ff6f91;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.login-box button:hover {
  background-color: #ec4e6c;
}
</style>